  function ocell = out_moments( G1 , HH , SDX , ZZ , sercell , shocell, Y ); 
% function ocell = out_moments( G1 , HH , SDX , ZZ , sercell , shocell, Y ); 
% 
% COMPUTE MOMENTS OF THE SOLUTION FROM GENSYS 
% 
% Compute the std, vcv, correlation, ac(1) and variance decomposition
% matrix for the gensys solution to the system in the form 
% y(t) = ZZ*s(t)
% s(t) = G1*s(t-1) + HH*e(t)  V( e(t) ) = SDX'*SDX  
% 
% If data vector is provided compute the equivalent data moments 
% except for the variance decomposition 
% 
% Output is OCELL 
% 
% Alejandro Justiniano 6/19/2005 
% ======================================================================
nz = size( ZZ , 1 ); 
nx = size( SDX , 1 ); 

temp = max( abs( eig(G1) ) ); 
if temp > 0.98  
    warning('Warning! Root of G1 is above 0.98 numerical problems may occur'); 
end 

if nargin < 7 | isempty( Y ) 
    
    flag_data = 0 ; 
    nr = 2; 
    
else 

    flag_data = 1; 
    nr = 3; 
    
    if size( Y , 2 ) ~= nz 
        error('Mistmatch matrix of observables and data') 
    end 
    
    ycov = cov( Y ); 
    yvar = ( diag( ycov ) )'; 
    ystd = sqrt( yvar ); 
    ycorr = corrcoef( Y ); 
    yacone = accov( Y , 1);
    
end 

sercell = sercell(:); 
shocell = shocell(:); 

if length( shocell ) ~= nx 
    error('Mistmatch length SHOCELL and SDX HH matrices') 
end 
if length( sercell ) ~= nz 
    error('Mistmatch length SERCELL and zmat matrix')
end 

ocell = emptycell( 2*nr + 2 , nz + 1 ); 
ocell(1,1) = {'MOMENTS'}; 

basecell = emptycell( nr , nz + 1); 
basecell(1,2:end) = sercell' ; 
basecell(2,1) = {'MODEL'}; 

if flag_data == 1 
    basecell(3,1) = {'DATA'}; 
end 

% ==================================
% Compute moments implied by model %
% ==================================
[vdcom,vcfull,sdevf,ACzero,ACone]=vardecom_mode(G1,HH,SDX,ZZ); 

% STANDARD DEVIATION 
% ------------------
basecell(1,1)     = {'STANDARD DEV'}; 
basecell(2,2:end) = num2cprec( sdevf' ); 

if flag_data == 1 
    basecell(3,2:end) = num2cprec( ystd );
end 

ocell(2:nr+1,:) = basecell; 

basecell(1,1)     = {'VARIANCE'}; 
basecell(2,2:end) = num2cprec( (diag( vcfull ))' ); 

% VARIANCE  
% ---------
if flag_data == 1 
    basecell(3,2:end) = num2cprec( yvar );
end 

ocell( nr+3:end,:) = basecell ; 

clear basecell; 

matcell = emptycell( nz + 2 , nz + 1 ); 
matcell(3:end,1) = sercell; 
matcell(2,2:end) = sercell'; 

tempc = emptycell( nz , nz ); 

indic = find( tril( ones(nz) ) ~= 0 ); 

% AC MATRIX
% ---------
tempc(indic) = num2cprec( ACzero(indic) ); 
matcell(1:2,1) = { 'CORRELATION','MODEL' }; 
matcell(3:end,2:end) = tempc ; 

ocell = merge_cells( ocell, matcell , 1);

if flag_data == 1 
    tempc(indic) = num2cprec( ycorr( indic ) ); 
    matcell(2,1) = {'DATA'}; 
    matcell( 3:end, 2:end ) = tempc ; 
    ocell = merge_cells( ocell, matcell , 1);
end 
clear tempc indic ; 

% CORRELATION LAG ONE 
% -------------------
matcell(1:2,1) = { 'AC LAG ONE','MODEL' }; 
matcell(3:end,2:end) = num2cprec( ACone ); 

ocell = merge_cells( ocell, matcell , 1); 

if flag_data == 1 
    matcell(1:2,1) = { 'AC LAG ONE','DATA' }; 
    matcell(3:end,2:end) = num2cprec( yacone ); 
    ocell = merge_cells( ocell, matcell , 1);  
end 

clear matcell; 

% VARIANCE DECOMPOSITIONS 
% ========================
vcell = emptycell( nz + 3, nx + 1); 
vcell(1,1:2)={'VARIANCE','DECOMPOSTION'}; 
vcell(2,1) = {'MODEL'}; 
vcell(2,2)  ={'Shocks'}; 
vcell(3,2:end) = shocell'; 
vcell(3,1) = {'Series'}; 
vcell(4:end,1) = sercell; 
vcell(4:end,2:end) = num2cprec( vdcom ); 

ocell = merge_cells( ocell, vcell, 1); 
